//
//  ViewController.m
//  05-UIScrollView
//
//  Created by 李昊林 on 16/7/20.
//  Copyright © 2016年 李昊林. All rights reserved.
//

#import "ViewController.h"
#import "AppDelegate.h"


// 宏定义
// 宏嵌套
// 宏就是系统编译时,代码的替换(预编译指令)
#define kScreenBounds [UIScreen mainScreen].bounds
// 1.全大写
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
// 2. k + 驼峰命名法
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:kScreenBounds];
    scrollView.backgroundColor = [UIColor colorWithRed:0.722 green:1.000 blue:0.817 alpha:1.000];
    [self.view addSubview:scrollView];
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 10, 300, 100);
    [button.backgroundColor = [UIColor redColor]];
    [scrollView addSubview:button];
    
    
    // 1.内容的范围 VGSizeZero  CGSizeMake(0, 0)
    // contentSize 设置可以滚动的范围
    scrollView.contentSize = CGSizeMake(0, 1000);
    // contentOffset 偏移量,滚动的范围
    // 不设置contentSize,contentOffset也可以改变
    // 问题,改变:contentOffset什么变了?
    // 答案: 改变contentOffset, 实质上是对scrollView的Bounds进行修改
    NSLog(@"改变前frame:%@",NSStringFromCGRect(scrollView.frame));
    NSLog(@"改变前bounds:%@",NSStringFromCGRect(scrollView.bounds));
    scrollView.contentOffset
    = CGPointMake(0, 500);
    NSLog(@"改变后frame:%@",NSStringFromCGRect(scrollView.frame));
    NSLog(@"改变后bounds:%@",NSStringFromCGRect(scrollView.bounds));
    
    // 3.contentIset
    // 添加滚动区域四周的滚动范围
    scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    // 4.directionalLockEnabled
    // 锁定垂直或水平滚动(45°角, 锁会失效)
    scrollView.directionalLockEnabled = YES;
    // 5. bounces 回弹效果
    //    scrollView.bounces = NO;
    // 6.alawaysBounceVertical
    //在没有垂直方向的滚动范围时,保证垂直方向的垂直效果存在
    scrollView.alwaysBounceVertical = YES;
    // 7,alwaysBounceHorizontal
    //在没有水平方向的滚动范围时,保证水平方向的垂直效果存在
    scrollView.alwaysBounceHorizontal = YES;
    // 8.pagingEnabled 按页滚动
    scrollView.pagingEnabled = YES;
    // 9 视图时候能够滚动,默认是YES
    scrollView.scrollEnabled = YES;
    // 10.指示条 默认是YES
    scrollView.showsVerticalScrollIndicator = YES;
    scrollView.showsHorizontalScrollIndicator = YES;
    // 11. 滚动指示器的边距
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(100, 0, 0, 0);
    // 12.滚动指示器的颜色,背景,深色,浅色,通用
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
    //13.
    // tracking 用户是否开始拖动
    //    scrollView.tracking
    //dragging 用户时候正在拖动
    //    scrollView.dragging
    //decelerating 用户时候在减速中
    //    scrollView.decelerating
    // 14.滚动视图,双击状态栏是否存在 默认 YES
    scrollView.scrollsToTop = YES;
    //15.scrollView 的代理
    scrollView.delegate = self;
    //16.键盘消失模式
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
    //测试
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 350, WIDTH - 20, 200)];
    textField.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:textField];
    //17.缩放系数
    //如果想进行视图缩放，必须实现viewForZoomingInScrollView：
    scrollView.minimumZoomScale = 0.5;
    scrollView.maximumZoomScale = 2;
    
}
//与其想要放大的子视图绑定，scrollView的contentSize会跟随这个子视图变化
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}
// 滚动视图滚动,就会执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  //  NSLog(@"滚动中");
    //Trackong 用户触摸
    if (scrollView.tracking) {
        NSLog(@"正在拖拽滚动");
        
//    } else {
//        NSLog(@"自己滚动");
    }
    //Dragging 用户开始滑动
    if (scrollView.dragging) {
        NSLog(@"");
    }
    //Decelerating 用户触摸结束
    if (scrollView.decelerating) {
        NSLog(@"");
    }
}

// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"开始拖拽");

}

// 将要结束拖拽
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    

}

// 已经结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"已经结束拖拽");

}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
